package com.mingtu.jgspatrol.activity;

import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.base.BaseBindingActivity;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.ActivityUtil;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.common.utils.ScreenUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.databinding.ActivityParkManageBinding;
import com.mingtu.jgspatrol.utils.MyConstant;

import java.util.HashMap;

public class ParkManageActivity extends BaseBindingActivity<ActivityParkManageBinding> {
    private String parkCode;
    private String parkNum;
    private String[] dateArray = {"自动审核", "人工审核"};
    private int checkPosition;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_park_manage;
    }

    @Override
    protected void initView() {
        setModuleTitle("停车场管理");
        setDarkThemeStatusBar();
        addClickListener(this, binding.button, binding.tvAuditModle);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getData() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.button:
                parkNum = MyUtills.getEditText(binding.editParkNum);
                if (StringUtils.isEmpty(parkNum)) {
                    ToastUtils.showLong("请输入车位数量！");
                    return;
                }
                parkSet();
                break;
            case R.id.tv_audit_modle:
                showPop();
                break;
        }
    }

    /**
     * 删除白名单
     */
    private void parkSet() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("parkId", binding.parkFilterView.getParkCode());
        hashMap.put("reservationNum", parkNum);
        hashMap.put("verifyBy", checkPosition);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_MODIFY_PARK_INFO)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(ActivityUtils.getTopActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            JSONObject jsonObject = JSON.parseObject(result);
                            String msg = jsonObject.getString("msg");
                            if (msg.equals(Constant.RESULT_SUCCESS)) {
                                ToastUtils.showLong("提交成功！");
                            }
                        }
                    }
                });

    }

    /**
     * 显示审核方式列表
     */
    private void showPop() {
        new XPopup.Builder(ActivityUtil.getTopActivity())
                .hasShadowBg(true)
                .maxHeight(ScreenUtils.getScreenHeight() / 2)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .asCenterList("请选择停车场", dateArray,
                        null, checkPosition,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                checkPosition = position;
                                binding.tvAuditModle.setText(dateArray[position]);
                            }
                        }).show();

    }
}